<?php

namespace app\admin\controller;
use app\common\model\zdjr\Project;
use app\common\model\zdjr\ProjectBot;
use app\common\model\zdjr\Rule;
use think\facade\Db;
use app\constants\Bot;

class Zdjrrule extends Botbase
{
    /**
     * @var Rule
     */
    protected $model;
    /**
     * @var Project
     */
    private $projectM;
    /**
     * @var \app\admin\model\Bot
     */
    private $botM;
    /**
     * @var ProjectBot
     */
    private $projectBotM;

    public function initialize()
    {
        parent::initialize(); // TODO: Change the autogenerated stub
        $this->model = new Rule();
        $this->projectM = new Project();
        $this->projectBotM = new ProjectBot();
        $this->botM = new \app\admin\model\Bot();
    }

    /**
     * 设置
     * @return mixed
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @author: fudaoji<fdj@kuryun.cn>
     */
    public function index(){
        if (request()->isPost()) {
            $post_data = input('post.');
            $where = ['admin_id' => $this->adminInfo['id']];
            $total = $this->model->total($where, true);
            if ($total) {
                $list = $this->model->getList(
                    [$post_data['page'], $post_data['limit']], $where,
                    ['id' => 'asc'], true, true
                );
                foreach ($list as $k => &$v){
                    $v['bots'] = implode(',', array_values($this->getBots(['id' => ['in', str_replace('i', '', $v['bots'])]])));
                    $_rules = json_decode($v['rules'], true);
                    $rule_str = '';
                    foreach (Rule::ruleFields() as $k1 => $v1){
                        if($k1 == 'groups' && $_rules[$k1]){
                            $_rules[$k1] = implode(',', array_values($this->getGroups(['wxid' => ['in', $_rules[$k1]]])));
                        }elseif ($k1 == 'invite_way' && isset($_rules[$k1])){
                            $_rules[$k1] = Rule::inviteWays($_rules[$k1]);
                        }elseif ($k1 == 'add_way' && isset($_rules[$k1])){
                            $_rules[$k1] = Rule::addWays($_rules[$k1]);
                        }elseif ($k1 == 'group_way' && isset($_rules[$k1])){
                            $_rules[$k1] = Rule::groupWays($_rules[$k1]);
                        }
                        isset($_rules[$k1]) && $rule_str .= '<p>'.$v1.'：<em>'.$_rules[$k1].'</em></p>';
                    }
                    $v['rules'] = $rule_str;
                    $list[$k] = $v;
                }
            } else {
                $list = [];
            }
            $this->success('success', '', ['total' => $total, 'list' => $list]);
        }

        $builder = new ListBuilder();
        $builder->addTopButton('addnew')
            //->setTip("同一时间只能执行一个任务")
            ->addTableColumn(['title' => '任务名称', 'field' => 'title', 'minWidth' => 90])
            ->addTableColumn(['title' => '所属项目', 'field' => 'project_id', 'minWidth' => 90,'type' => 'enum', 'options' => $this->projectM->getProjects()])
            ->addTableColumn(['title' => '执行机器人', 'field' => 'bots', 'minWidth' => 200])
            ->addTableColumn(['title' => '规则', 'field' => 'rules', 'type' => 'article', 'minWidth' => 100])
            ->addTableColumn(['title' => '状态', 'field' => 'status', 'minWidth' => 80,'type' => 'enum','options' => [0 => '禁用', 1=> '启用']])
            ->addTableColumn(['title' => '操作', 'minWidth' => 200, 'type' => 'toolbar'])
            ->addRightButton('edit')
            ->addRightButton('self',['title' => '禁用/启用', 'href' => url('forbidPost', ['id' => '__data_id__']), 'data-ajax' => 1, 'data-confirm' => 1])
            ->addRightButton('delete');
        return $builder->show();
    }

    /**
     * 编辑
     * @return mixed
     * @throws \think\Exception
     * Author: fudaoji<fdj@kuryun.cn>
     */
    public function edit(){
        $id = input('id', null);
        $data = $this->model->getOneByMap(['id' => $id, 'admin_id' => $this->adminInfo['id']], true, true);

        if (!$data) {
            $this->error('参数错误');
        }

        $rules = json_decode($data['rules'], true);
        $rules['groups'] = explode(',', $rules['groups']);
        $data = array_merge($data, $rules);
        $data['bots'] = explode(',', str_replace('i', '', $data['bots']));
        $groups = model('admin/botMember')->getField(['wxid','nickname'], ['type' => Bot::GROUP]);
        $bots = $this->getFreeBots($id, $data['project_id']);

        $builder = new FormBuilder();
        $builder->setPostUrl(url('savePost'))
            ->setTip("文本内容遵循以下替换规则：<ul><li>[名称]：最终会被替换为名单中的名称</li><li>[电话]：最终会被替换为名单中的电话</li></ul>")
            ->addFormItem('id', 'hidden', 'id', 'id')
            ->addFormItem('project_id', 'chosen', '选择项目', '选择项目', $this->projectM->getProjects(['admin_id' => $this->adminInfo['id']]))
            ->addFormItem('title', 'text', '任务名称', '50字内', [], 'required maxlength=50')
            ->addFormItem('bots', 'chosen_multi', '执行机器人', '请选择执行机器人', $bots, 'required')
            ->addFormItem('apply_limit', 'number', '打招呼限制', '0表示不限', [], 'required min=0')
            ->addFormItem('busy_stop', 'radio', '账号频繁不再添加', '请求添加验证消息', [1 => '是', 0 => '否'], 'maxlength=200')
            ->addFormItem('status', 'radio', '状态', '状态', [1 => '启动', 0 => '停止'], 'required')
            ->addFormItem('add_legend', 'legend', '加人策略', '加人策略')
            ->addFormItem('speed', 'number', '每次添加个数', '达到设置值后自动切换下一个账号，建议3-5个之间，多了容易封号', [], 'required min=1')
            ->addFormItem('time_add', 'text', '添加间隔时长/秒', '相邻两次请求的间隔时间，请填写时间段，例如：10-15，单位秒', [], 'required')
            ->addFormItem('time_round', 'number', '每轮间隔时长/分', '所有机器人执行一次为一轮，单位分，建议10分钟以上', [], 'required min=1')
            ->addFormItem('add_msg', 'text', '验证消息', '请求添加验证消息，内容中可填入[名称]，最终会被替换为名单中的名称', [], 'maxlength=200')
            ->addFormItem('add_way', 'chosen', '添加方式', '添加方式', $this->model->addWays())
            ->addFormItem('after_legend', 'legend', '通过后策略', '通过后策略')
            ->addFormItem('remark_name', 'text', '备注名称', '留空则不自动备注', [], 'maxlength=50')
            ->addFormItem('send_card', 'text', '发送名片', '请填写wxid')
            ->addFormItem('group_way', 'radio', '拉群方式', '拉群方式', [0 => '不进群', 1 => '自动建群', 2 => '自动拉群'])
            ->addFormItem('group_name', 'text', '建群名称', '针对自动建群时有效')
            ->addFormItem('group_members', 'text', '需要拉好友', '填写好友wxid', [])
            ->addFormItem('group_quit', 'radio', '是否退群', '是否退群', [1 => '是', 0 => '否'])
            ->addFormItem('groups', 'chosen_multi', '自动拉群', '留空则不自动拉群', $groups)
            ->addFormItem('invite_way', 'radio', '入群方式', '入群方式', Rule::inviteWays(), 'min=1 max=500')
            ->addFormItem('group_person_limit', 'number', '群自动切换', '群成员满多少人自动切换', [], 'min=1 max=500')
            ->setFormData($data);

        return $builder->show();
    }

    /**
     * 新增
     * @return mixed
     * @throws \think\Exception
     * Author: fudaoji<fdj@kuryun.cn>
     */
    public function add(){
        $step = input('step', 1);
        $builder = new FormBuilder();
        if($step == 1){
            $builder->setPostUrl(url('add', ['step' => 1]))
                ->setBtnSubmit(['text' => '下一步'])
                ->setBtnReset(['show' => 0])
                ->addFormItem('project_id', 'chosen', '选择项目', '选择项目', $this->projectM->getProjects(['admin_id' => $this->adminInfo['id']]));
            if(request()->isPost()){
                $this->success('请继续配置其他信息', url('add', ['step' => 2, 'project_id' => input('project_id')]));
            }
        }else{
            $project_id = input('project_id');
            if(empty($project_id)){
                $this->error('请先选择项目');
            }
            $data = [
                'speed' => 3,
                'time_add' => '10-15',
                'time_round' => '5',
                'add_msg' => '[名称]，你好',
                'busy_stop' => 1,
                'invite_way' => 'link',
                'project_id' => $project_id,
                'apply_limit' => 10,
                'group_way' => 0,
                'group_quite' => 0
            ];
            $groups = model('admin/botMember')->getField(['wxid','nickname'], ['type' => Bot::GROUP]);
            $bots = $this->getFreeBots(0, $project_id);
            $builder->setPostUrl(url('savePost'))
                ->setTip("文本内容遵循以下替换规则：<ul><li>[名称]：最终会被替换为名单中的名称</li><li>[电话]：最终会被替换为名单中的电话</li></ul>")
                ->addFormItem('project_id', 'chosen', '选择项目', '选择项目', $this->projectM->getProjects(['admin_id' => $this->adminInfo['id'], 'id' => $project_id]))
                ->addFormItem('title', 'text', '任务名称', '50字内', [], 'required maxlength=50')
                ->addFormItem('bots', 'chosen_multi', '执行机器人', '请选择执行机器人', $bots, 'required')
                ->addFormItem('apply_limit', 'number', '打招呼限制', '0表示不限', [], 'required min=0')
                ->addFormItem('busy_stop', 'radio', '账号频繁不再添加', '请求添加验证消息', [1 => '是', 0 => '否'], 'maxlength=200')
                ->addFormItem('add_legend', 'legend', '加人策略', '加人策略')
                ->addFormItem('speed', 'number', '每次添加个数', '达到设置值后自动切换下一个账号，建议3-5个之间，多了容易封号', [], 'required min=1')
                ->addFormItem('time_add', 'text', '添加间隔时长/秒', '相邻两次请求的间隔时间，请填写时间段，例如：10-15，单位秒', [], 'required')
                ->addFormItem('time_round', 'number', '每轮间隔时长/分', '所有机器人执行一次为一轮，单位分，建议10分钟以上', [], 'required min=1')
                ->addFormItem('add_msg', 'text', '验证消息', '请求添加验证消息，内容中可填入[名称]，最终会被替换为名单中的名称', [], 'maxlength=200')
                ->addFormItem('add_way', 'chosen', '添加方式', '添加方式', $this->model->addWays())
                ->addFormItem('after_legend', 'legend', '通过后策略', '通过后策略')
                ->addFormItem('remark_name', 'text', '备注名称', '留空则不自动备注', [], 'maxlength=50')
                ->addFormItem('send_card', 'text', '发送名片', '请填写wxid')
                ->addFormItem('group_way', 'radio', '拉群方式', '拉群方式', [0 => '不进群', 1 => '自动建群', 2 => '自动拉群'])
                ->addFormItem('group_name', 'text', '建群名称', '针对自动建群时有效')
                ->addFormItem('group_members', 'text', '需要拉好友', '填写好友wxid', [])
                ->addFormItem('group_quit', 'radio', '是否退群', '是否退群', [1 => '是', 0 => '否'])
                ->addFormItem('groups', 'chosen_multi', '自动拉群', '针对自动拉群，留空则不自动拉群', $groups)
                ->addFormItem('invite_way', 'radio', '入群方式', '入群方式', Rule::inviteWays(), 'min=1 max=500')
                ->addFormItem('group_person_limit', 'number', '群自动切换', '群成员满多少人自动切换', [], 'min=1 max=500')
                ->setFormData($data);
        }

        return $builder->show();
    }

    private function getFreeBots($id = 0, $project_id = 0){
        $where = ['id' => ['in', [0]]];
        if($project_id){
            $exist = $this->projectBotM->getField('bot_id', ['admin_id' => $this->adminInfo['id'], 'project_id' => $project_id], true);
            $exist && $where['id'] = ['in', $exist];
        }
        //dump($where);exit;
        $bots = $this->getBots($where);

        if($bots){
            $rule_where = ['admin_id' => $this->adminInfo['id'], 'status' => 1];
            $id && $rule_where['id'] = ['<>', $id];
            $busy_bot = $this->model->getField('bots', $rule_where, true);
            $busy_bot_id = [];
            foreach ($busy_bot as $v){
                $v = str_replace('i', '', $v);
                $busy_bot_id = array_merge($busy_bot_id, explode(',', $v));
            }
            foreach ($bots as $k => $v){
                if(in_array($k, $busy_bot_id)){
                    unset($bots[$k]);
                }
            }
        }
        return $bots;
    }

    /**
     * 禁用/启用
     * @throws \think\Exception
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     * Author: fudaoji<fdj@kuryun.cn>
     */
    public function forbidPost()
    {
        $id = input('id', null);
        $data = $this->model->getOneByMap(['id' => $id, 'admin_id' => $this->adminInfo['id']], true, true);

        if (!$data) {
            $this->error('参数错误');
        }
        $this->model->updateOne([
            'id' => $id,
            'status' => abs($data['status'] - 1)
        ]);
        $this->success('操作成功');
    }

    public function savePost($jump_to = '/undefined', $data = [])
    {
        $post_data = input('post.');
        unset($post_data['__token__']);
        $project_id = $post_data['project_id'];
        $id = empty($post_data['id']) ? 0 : $post_data['id'];
        if(empty($post_data['bots'])){
            $this->error('请选择执行机器人!');
        }
        $botids = explode(',', $post_data['bots']);
        $data = [
            'id' => $id,
            'title' => $post_data['title'],
            'admin_id' => $this->adminInfo['id'],
            'bots' => implode('i,', $botids).'i',
            'project_id' => $project_id,
            'rules' => json_encode($post_data, JSON_UNESCAPED_UNICODE)
        ];
        parent::savePost($jump_to, $data); // TODO: Change the autogenerated stub
    }
}